//
//  UIView+DefaultView.m
//  New_Runway
//
//  Created by mack on 2018/2/22.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "UIView+DefaultView.h"
static char *DefaultKey = "DefaultKey";

@implementation UIView (DefaultView)
- (UIView *)bgView
{
    return objc_getAssociatedObject(self, DefaultKey);
}

- (void)setBgView:(UIView *)bgView
{
    objc_setAssociatedObject(self, DefaultKey , bgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)addDefaultViewWithText:(NSString *)defaultText defaultImageType:(DefaultImageType)type
{
    [self.bgView removeFromSuperview];
    
    self.bgView = [[UIView alloc] init];
    self.bgView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
    self.bgView.backgroundColor = ContentBackgroundColor();
    self.bgView.userInteractionEnabled = NO;
    // 默认图片
    UIImageView *iv = [[UIImageView alloc] init];
    switch (type) {
        case DefaultImageTypeNoData:
            [iv setImage:[UIImage imageNamed:@"emptry"]];
            break;
            
        case DefaultImageTypeNoWeb:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        case DefaultImageTypeNoContacts:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        case DefaultImageTypeNoMsg:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        default:
            break;
    }
    iv.frame = CGRectMake(self.bgView.size.width/2-iv.image.size.width/2, self.bgView.size.height/2-iv.image.size.height/2-30, iv.image.size.width, iv.image.size.height);
    [self.bgView addSubview:iv];
    
    // 默认文字
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(iv.frame)+20, Screen_width, 30)];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.text = defaultText;
    textLabel.textColor = UIColorHex(0x848484);
    [self.bgView addSubview:textLabel];
    
    [self addSubview:self.bgView];
    
}
/**默认图根据frame定*/
- (void)addDefaultViewWithText:(NSString *)defaultText defaultImageType:(DefaultImageType)type withFrame:(CGRect)frame
{
    [self.bgView removeFromSuperview];
    
    self.bgView = [[UIView alloc] initWithFrame:frame];
    self.bgView.backgroundColor = ContentBackgroundColor();
    self.bgView.userInteractionEnabled = NO;
    // 默认图片
    UIImageView *iv = [[UIImageView alloc] init];
    switch (type) {
        case DefaultImageTypeNoData:
            [iv setImage:[UIImage imageNamed:@"emptry"]];
            break;
            
        case DefaultImageTypeNoWeb:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        case DefaultImageTypeNoContacts:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        case DefaultImageTypeNoMsg:
            [iv setImage:[UIImage imageNamed:@""]];
            break;
            
        default:
            break;
    }
    iv.frame = CGRectMake(self.bgView.size.width/2-iv.image.size.width/2, self.bgView.size.height/2-iv.image.size.height/2-30, iv.image.size.width, iv.image.size.height);
    [self.bgView addSubview:iv];
    
    // 默认文字
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(iv.frame)+20, Screen_width, 30)];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.text = defaultText;
    textLabel.textColor = UIColorHex(0x848484);
    [self.bgView addSubview:textLabel];
    
    [self addSubview:self.bgView];
    
}

- (void)removeDefaultView
{
    [self.bgView removeFromSuperview];
}
@end
